package com.styytech.dadabao.widge.photoview.myzoomview;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.styytech.dadabao.widge.photoview.PhotoViewAttacher;
import com.styytech.dadabao.R;

/**
 * 单张图片显示Fragment
 */
public class ImageDetailFragment extends Fragment {
    private String mImageUrl;
    private ImageView mImageView;
    private ImageView progressBar;
    private PhotoViewAttacher mAttacher;
    private boolean isIndex;
    private boolean isZoom;

    public static ImageDetailFragment newInstance(String imageUrl,
                                                  boolean isIndex, boolean isZoom) {
        final ImageDetailFragment f = new ImageDetailFragment();

        final Bundle args = new Bundle();
        args.putString("url", imageUrl);
        args.putBoolean("isIndex", isIndex);
        args.putBoolean("isZoom", isZoom);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImageUrl = getArguments() != null ? getArguments().getString("url")
                : null;
        isIndex = getArguments() != null ? getArguments().getBoolean("isIndex")
                : true;
        isZoom = getArguments() != null ? getArguments().getBoolean("isZoom")
                : true;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.image_detail_fragment,
                container, false);

        FrameLayout fl_zoomPicture = (FrameLayout) v
                .findViewById(R.id.fl_zoomPicture);


        if (isIndex) {
            fl_zoomPicture.setBackgroundColor(Color.BLACK);
        }

        mImageView = (ImageView) v.findViewById(R.id.image);
        mAttacher = new PhotoViewAttacher(mImageView);
        // 设置图片是否可以缩放
        mAttacher.setZoomable(isZoom);
        mAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {

            @Override
            public void onPhotoTap(View arg0, float arg1, float arg2) {
                getActivity().finish();
            }
        });

        progressBar = (ImageView) v.findViewById(R.id.loading);
        if (progressBar != null) {
            AnimationDrawable anim = (AnimationDrawable) progressBar
                    .getBackground();
            if (anim != null) {
                anim.start();
            }
        }
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        DisplayImageOptions options;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)// 设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
                .resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                        // .displayer(new FadeInBitmapDisplayer(100))// 是否图片加载好后渐入的动画时间
                .build();// 构建完成
        ImageLoader.getInstance().displayImage(mImageUrl, mImageView, options,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        progressBar.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view,
                                                FailReason failReason) {
                        String message = null;
                        switch (failReason.getType()) {
                            case IO_ERROR:// 下载错误
                                message = "图片显示失败";
                                break;
                            case DECODING_ERROR:// 图片无法显示
                                message = "图片显示失败";
                                break;
                            case NETWORK_DENIED:// 网络有问题，无法下载
                                message = "网络有问题，图片显示失败";
                                break;
                            case OUT_OF_MEMORY:// 图片太大无法显示
                                message = "图片显示失败";
                                break;
                            case UNKNOWN:// 未知的错误
                                message = "图片显示失败";
                                break;
                        }
                        Toast.makeText(getActivity(), message,
                                Toast.LENGTH_SHORT).show();
                        progressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view,
                                                  Bitmap loadedImage) {
                        progressBar.setVisibility(View.GONE);
                        mAttacher.update();
                    }
                });
    }
}
